var QQMapWX = require('../../../lib/qqmap-wx-jssdk.js')
var qqmapssdk = new QQMapWX({
  key: 'XEFBZ-BFQKG-QCXQM-IUH47-AZMI7-BHBIP'
})

import { HttpUtil, Target } from '../../../utils/http_util.js'
// pages/user/adressManage/main.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    customerId:'',
    
    locationInfo: '',//当前定位
    addressList: [ //'0'-'女','1'-'男'
      { 
        addressId: 1, address: "四川省成都市武侯区天府大道北段1700号",
        name: "小雯", six: '0', phone: '13333333333' },
      {
        addressId: 2, address: "四川省成都市武侯区天府大道北段1700号",
        name: "小雯", six: '1', phone: '13333333333'
      },
      {
        addressId: 3, address: "四川省成都市武侯区天府大道北段1700号",
        name: "小雯", six: '0', phone: '13333333333'
      },
      {
        adressId: 4, address: "四川省成都市武侯区天府大道北段1700号",
        name: "小雯", six: '0', phone: '13333333333'
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.loadData();
    wx.getStorage({
      key: 'locationInfo',
      success(lores) {
        if (lores === null || lores.data === null || lores.data.lid === null || lores.data.lid === '') {
          that.getLocation();
        }else{
          that.setData({
            locationInfo: lores.data.addr
          });
        }
      },
      fail() {
        that.getLocation();
      }
    })
  },
  loadData:function(){
    let that = this;
    var consumerId = 1;
    HttpUtil.get(Target.self, "customerAddress/list/" + consumerId).then(res => {
      console.log(res);
      that.setData({
        addressList: res
      });
    })
  },
  reloadLoaction:function(e){
    this.getLocation();
  },
  getLocation: function (e) {
    var that = this;
    wx.getLocation({
      type: 'gcj02',
      success: function (result) {
        console.log('location', result);
        var locationString = result.latitude + "," + result.longitude;
        
        qqmapssdk.reverseGeocoder({
          location: locationString,
          success: function (res) {
            that.setData({
              locationInfo: res.result.address
            })
            try {
              var locationInfo = {
                lid: 0,  //0代表当前位置
                latitude: result.latitude,
                longitude: result.longitude,
                addr: res.result.address
              }
              wx.setStorageSync('locationInfo', locationInfo)

            } catch (e) {
              console.log(e)
            }
          }
        })
      }
    })
  },
  goAddAddress:function(e){
    wx.navigateTo({
      url: "/pages/user/addAddress/addAddress",
    })
  },
  chooseAddress:function(e){
    let id = e.currentTarget.dataset.id;
    console.log(id);
    let addressList = this.data.addressList;
    for (let i = 0; i < addressList.length; i++){
      if(addressList[i].addressId == id){
        let address = addressList[i];
        var locationInfo = {
          lid: id,  //0代表当前位置
          latitude: address.lat,
          longitude: address.lng,
          addr: address.address
        }
        console.log(locationInfo);
        wx.setStorageSync('locationInfo', locationInfo);
        break;
      }
    }
    wx.reLaunch({
      url: '/pages/index/index-main/main'
    })
  },
  updateAddress:function(e){
    let id = e.currentTarget.dataset.id;
    let address={};
    let addressList = this.data.addressList;
    for (let i = 0; i < addressList.length; i++) {
      if (addressList[i].addressId == id) {
        address = addressList[i];
        break;
      }
    }
    console.log(address)
    wx.navigateTo({
      url: "/pages/user/updateAddress/updateAddress?address=" + address.address +"&addressId="+ address.addressId + "&consumerId=" + address.consumerId + "&lat="+address.lat+"&lng="+address.lng+"&name="+address.name+"&phone="+address.phone+"&sex="+address.sex+"&type="+address.type
    })
  },
  deleteAddress:function(e){
    var that = this;
    let id = e.currentTarget.dataset.id;
    let addressList = this.data.addressList;
    let addressId='';
    for (let i = 0; i < addressList.length; i++) {
      if (addressList[i].addressId == id) {
        addressId = addressList[i].addressId;
        break;
      }
    }
    HttpUtil.delete(Target.self, "customerAddress/delete/"+addressId).then(res => {
      that.loadData();
    })
  }
})